package com.my12306.train.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my12306.common.constant.CommonConstants;
import com.my12306.common.util.SnowflakeIdWorker;
import com.my12306.common.util.response.ResponseData;
import com.my12306.train.dao.CariageMarshallingRelationDao;
import com.my12306.train.dao.CarriageDao;
import com.my12306.train.dao.MarshallingDao;
import com.my12306.train.dao.TrainDao;
import com.my12306.train.model.entity.CariageMarshallingRelationEntity;
import com.my12306.train.model.entity.CarriageEntity;
import com.my12306.train.model.entity.TrainEntity;
import com.my12306.train.model.vo.CarriageMarshallingRelationVo;
import com.my12306.train.model.vo.CarriageVo;
import com.my12306.train.model.vo.MarshallingCarriageVo;
import com.my12306.train.service.CariageMarshallingRelationService;
import com.my12306.train.service.CarriageService;
import com.my12306.train.service.MarshallingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;


@Service("cariageMarshallingRelationService")
public class CariageMarshallingRelationServiceImpl extends ServiceImpl<CariageMarshallingRelationDao, CariageMarshallingRelationEntity> implements CariageMarshallingRelationService {
    @Resource
    CarriageDao carriageDao;
    @Resource
    MarshallingDao marshallingDao;
    @Resource
    TrainDao trainDao;
    @Autowired
    CarriageService carriageService;
    @Autowired
    MarshallingService marshallingService;

    @Override
    public Long saveCarriageMarshallingRelation(CarriageMarshallingRelationVo carriageMarshallingRelationVo) {
        if(carriageMarshallingRelationVo != null){
            CariageMarshallingRelationEntity cariageMarshallingRelationEntity = new CariageMarshallingRelationEntity();
            cariageMarshallingRelationEntity.setId(SnowflakeIdWorker.generateId());
            cariageMarshallingRelationEntity.setCreateDate(new Date());
            cariageMarshallingRelationEntity.setUpdateDate(new Date());
            cariageMarshallingRelationEntity.setGroupId(carriageMarshallingRelationVo.getGroupId());
            cariageMarshallingRelationEntity.setCarriageId(carriageMarshallingRelationVo.getCarriageId());
            int insert = this.baseMapper.insert(cariageMarshallingRelationEntity);
            System.out.println("成功添加车厢到编组的数量：" + insert);
            CarriageEntity carriageEntity = carriageDao.selectById(cariageMarshallingRelationEntity.getCarriageId());
            if (carriageEntity != null){
                carriageEntity.setCoachNumber(carriageMarshallingRelationVo.getCoachNumber());
                carriageService.updateCarriageById(carriageEntity);
            }
        }
        return null;
    }

    @Override
    public void updateCarriageMarshallingRelation(CarriageMarshallingRelationVo carriageMarshallingRelationVo) {
        if(carriageMarshallingRelationVo != null){
            CariageMarshallingRelationEntity cariageMarshallingRelationEntity = new CariageMarshallingRelationEntity();
            cariageMarshallingRelationEntity.setId(carriageMarshallingRelationVo.getId());
            CariageMarshallingRelationEntity cariageMarshallingRelationEntity1 = this.baseMapper.selectById(cariageMarshallingRelationEntity);
            cariageMarshallingRelationEntity1.setGroupId(carriageMarshallingRelationVo.getGroupId());
            cariageMarshallingRelationEntity1.setCarriageId(carriageMarshallingRelationVo.getCarriageId());
            this.baseMapper.updateById(cariageMarshallingRelationEntity1);

            CarriageEntity carriageEntity = carriageDao.selectById(carriageMarshallingRelationVo.getCarriageId());
            CarriageEntity selectCarriageEntity = carriageDao.selectById(carriageMarshallingRelationVo.getSelectCarriage());
            if (carriageEntity != null){
                carriageEntity.setCoachNumber(carriageMarshallingRelationVo.getCoachNumber());
                carriageService.updateCarriageById(carriageEntity);
            }
            if (selectCarriageEntity != null){
                selectCarriageEntity.setCoachNumber("0");
                carriageService.updateCarriageById(selectCarriageEntity);
            }
        }
    }

    @Override
    public ResponseData deleteMarshallingBatchByIds(List<Long> ids) {
        //更新该编组下所有车厢的车厢表指定carriage_id车厢的车厢号为0
        ids.stream().forEach(group_id->{
            System.out.println(group_id);
            MarshallingCarriageVo marshallingCarriageVo = marshallingService.queryAllMarshallingById(group_id);
            if(marshallingCarriageVo != null){
                List<CarriageVo> carriageEntityList = marshallingCarriageVo.getCarriageEntityList();
                System.out.println(carriageEntityList);
                carriageEntityList.stream().forEach(carriageVo -> {
                    System.out.println(carriageVo);
                    CarriageEntity carriageEntity = new CarriageEntity();
                    BeanUtils.copyProperties(carriageVo, carriageEntity);
                    System.out.println(carriageEntity);
                    carriageEntity.setCoachNumber("0");
                    carriageEntity.setUpdateDate(new Date());
                    carriageDao.updateById(carriageEntity);
                });
            }
        });
        //更新绑定了该编组的列车为未绑定编组状态（group_id=null）
        ids.stream().forEach(group_id->{
            QueryWrapper<TrainEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("logic_delete",CommonConstants.LogicDelete.no_delet.getValue());
            queryWrapper.eq("group_id",group_id);
            TrainEntity trainEntity = trainDao.selectOne(queryWrapper);
            if (trainEntity != null){
                System.out.println(trainEntity);
                trainEntity.setGroupId(0L);
                trainEntity.setUpdateDate(new Date());
                trainDao.updateById(trainEntity);
            }
        });
        //删除编组表的指定group_id编组
        marshallingDao.deleteMarshallingBatchByIds(ids, CommonConstants.LogicDelete.delete.getValue());
        //删除车厢编组关联表的指定group_id数据
        this.baseMapper.deleteCarriageMarshallingRelationBatchByGroupIds(ids, CommonConstants.LogicDelete.delete.getValue());
        return ResponseData.ok();
    }
}
